import { Controller, Get, Post, Body, Param, UseGuards, Request, Put } from '@nestjs/common';
import { MatchService } from './match.service';
import { CreateMatchDto } from './dto/create-match.dto';
import { CreateMatchPreferenceDto } from './dto/create-match-preference.dto';
import { UpdateMatchPreferenceDto } from './dto/update-match-preference.dto';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { UserRecommendationDto } from '../users/dto/user-recommendation.dto';

@Controller('match')
@UseGuards(JwtAuthGuard)
export class MatchController {
  constructor(private readonly matchService: MatchService) {}

  // 获取推荐的匹配用户
  @Get('recommendations')
  async getRecommendations(@Request() req) {
    const userId = req.user.userId;
    const recommendations: UserRecommendationDto[] = await this.matchService.getRecommendations(userId);
    
    return {
      message: '获取推荐用户成功',
      data: recommendations.map(user => ({
        id: user.id,
        name: user.name,
        age: user.age,
        gender: user.gender,
        location: user.location,
        bio: user.bio,
        interests: user.interests,
        hobbies: user.hobbies,
        education: user.education,
        occupation: user.occupation,
        maritalStatus: user.maritalStatus,
        hasChildren: user.hasChildren,
        wantsChildren: user.wantsChildren,
        smoking: user.smoking,
        drinking: user.drinking,
        diet: user.diet,
        hasPets: user.hasPets,
        pets: user.pets,
        values: user.values,
        religion: user.religion,
        politicalView: user.politicalView,
        relationshipGoal: user.relationshipGoal,
        languages: user.languages,
        // 匹配度分数
        compatibility: user.compatibilityScore || Math.floor(Math.random() * 20) + 80, // 如果没有计算出匹配度，则生成随机数
        // 随机头像和距离（可以在未来版本中替换为真实数据）
        avatar: `https://randomuser.me/api/portraits/${user.gender === 'female' ? 'women' : 'men'}/${Math.floor(Math.random() * 100)}.jpg`,
        distance: `${(Math.random() * 10).toFixed(1)}km`,
      })),
    };
  }

  // 创建匹配（喜欢或不喜欢）
  @Post()
  async createMatch(@Request() req, @Body() createMatchDto: CreateMatchDto) {
    const userId = req.user.userId;
    const match = await this.matchService.createMatch(userId, createMatchDto);
    
    return {
      message: createMatchDto.liked ? '已喜欢该用户' : '已跳过该用户',
      data: match,
      matched: match.matched,
    };
  }

  // 获取用户的匹配列表
  @Get('list')
  async getMatches(@Request() req) {
    const userId = req.user.userId;
    const matches = await this.matchService.getMatches(userId);
    
    return {
      message: '获取匹配列表成功',
      data: matches,
    };
  }

  // 获取用户的匹配偏好
  @Get('preference')
  async getPreference(@Request() req) {
    const userId = req.user.userId;
    const preference = await this.matchService.getPreference(userId);
    
    return {
      message: '获取匹配偏好成功',
      data: preference,
    };
  }

  // 创建或更新用户的匹配偏好
  @Post('preference')
  async createOrUpdatePreference(
    @Request() req,
    @Body() createMatchPreferenceDto: CreateMatchPreferenceDto,
  ) {
    const userId = req.user.userId;
    const preference = await this.matchService.createOrUpdatePreference(
      userId,
      createMatchPreferenceDto,
    );
    
    return {
      message: '更新匹配偏好成功',
      data: preference,
    };
  }

  // 更新用户的匹配偏好
  @Put('preference')
  async updatePreference(
    @Request() req,
    @Body() updateMatchPreferenceDto: UpdateMatchPreferenceDto,
  ) {
    const userId = req.user.userId;
    const preference = await this.matchService.updatePreference(
      userId,
      updateMatchPreferenceDto,
    );
    
    return {
      message: '更新匹配偏好成功',
      data: preference,
    };
  }
}